package com.yuan.controller.api;

import com.yuan.exception.BusinessException;
import com.yuan.result.R;
import com.yuan.result.ResponseEnum;
import com.yuan.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Api(tags = "阿里云文件管理")
//@CrossOrigin
@RestController
@RequestMapping("/api/oss/file")
public class FileController {

    @Resource
    private FileService fileService;

    /**
     * 文件上传
     */
    @ApiOperation("文件上传阿里云")
    @PostMapping("/upload")
    public R upload(
            @ApiParam(value = "文件", required = true)
            @RequestParam("file") MultipartFile file,

            @ApiParam(value = "模块", required = true)
            @RequestParam("module") String module)  {

        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            // 上传到阿里云
            String uploadUrl = fileService.upload(inputStream, module, originalFilename);
            //返回r对象
            return R.success().message("文件上传成功").data("url", uploadUrl);
        } catch (IOException e) {
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR, e);
        }
    }

    @ApiOperation("删除OSS文件")
    @DeleteMapping("/remove")
    public R remove(
            @ApiParam(value = "要删除的文件路径", required = true)
            @RequestParam("url") String url) {
        fileService.removeFile(url);
        return R.success().message("删除成功");
    }

    @ApiOperation("文件上传到本地")
    @PostMapping("/uploadLocal")
    public R uploadLocal(
            @ApiParam(value = "文件", required = true)
            @RequestParam("file") MultipartFile uploadFile)  {
        String uploadFileName = uploadFile.getOriginalFilename();

        try {
            //获取文件扩展名
            int index = uploadFileName.lastIndexOf(".");
            String exName = uploadFileName.substring(index);
            //随机生成文件名
            String uuid = UUID.randomUUID().toString();
            //将生成的uuid中的"-"去掉，并拼接扩展名
            String fileName = uuid.replace("-", "") + exName;

            //新建服务器接受文件的目录
            String realPath = "E:\\Poject\\2021-11-13\\vue-admin\\src\\assets\\upload";
            //路径转文件
            File file = new File(realPath);
            //如果文件不存在，新建文件夹
            if(!file.exists()){
                file.mkdirs();
            }
           // String realFileName = realPath + "/" + fileName;
            //拼接新文件路径
            File newFile = new File(realPath + "/" + fileName);
            //把临时文件copy过来
            // 实现文件的上传
            uploadFile.transferTo(newFile);

            /*// 12准备文件的虚拟路径
            // http://image.jt.com/file/2018/05/07/radweedwsaf210.jpg
           String urlPre = "http://image.com/";

            String urlPath = urlPre + realFileName;*/


            //返回r对象
            return R.success().message("文件上传成功").data("url", fileName);

        } catch (IOException e) {
            throw new BusinessException(ResponseEnum.UPLOAD_ERROR, e);
        }
    }

    @ApiOperation("删除本地上传文件")
    @DeleteMapping("/removeLocal")
    public R removeLocal(
            @ApiParam(value = "要删除的文件路径", required = true)
            @RequestParam("url") String url) {
        //新建服务器接受文件的目录
        String realPath = "F:\\Poject\\2021-11-13\\vue-admin\\src\\assets\\upload";
        File file = new File(realPath+"/"+url);
        if (file.isFile() && file.exists()) {
             file.delete();
            return R.success().message("删除成功");
        }
        return R.error().message("删除失败");
    }
}
